import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { GqlAuthGuard } from '@/common/guards/auth.guards';
import { FindOptionsWhere, Like } from 'typeorm';
import { UseGuards } from '@nestjs/common';
import { TempService } from './temp.service';
import { TempInput } from './dto/temp-input.type';
import { CurUserId } from '@/common/decorators/current-user.decorator';
import { CurOrgId } from '@/common/decorators/current-org.decorator';
import { SUCCESS } from '@/common/constants/code';
import { TempResult, TempResults } from './dto/result-temp.output';
import { TempType } from './dto/temp.type';
import { Temp } from './models/temp.entity';
import { PageInput } from '@/common/dto/page.input';
import { Result } from '@/common/dto/result.type';

@Resolver(() => TempType)
@UseGuards(GqlAuthGuard)
export class TempResolver {
  // 构造函数，注入TempService服务
  constructor(private readonly tempService: TempService) {}

  @Query(() => TempResult)
  async getTempInfo(@Args('id') id: string): Promise<TempResult> {
    const result = await this.tempService.findById(id);
    if (result) {
      return {
        code: SUCCESS,
        data: result,
        message: '获取成功',
      };
    }
    return {
      code: null,
      message: '不存在',
    };
  }
  @Mutation(() => TempResult, { description: '更新学生信息' })
  async updateTemp(
    @Args('params') params: TempInput,
    @CurUserId() userId: string,
    @CurOrgId() orgId: string,
    @Args('id', { nullable: true }) id: string,
  ): Promise<TempResult> {
    if (!id) {
      const res = await this.tempService.create({
        ...params,
        org: {
          id: orgId,
        },
        createdBy: userId,
      });
      if (res) {
        return {
          code: SUCCESS,
          message: '创建成功',
        };
      }
      return {
        code: null,
        message: '创建失败',
      };
    }
    const temp = await this.tempService.findById(id);
    if (temp) {
      const res = await this.tempService.updateById(temp.id, {
        ...params,
        updatedBy: userId,
      });
      if (res) {
        return {
          code: SUCCESS,
          message: '更新成功',
        };
      }
      return {
        code: null,
        message: '更新失败',
      };
    }
    return {
      code: null,
      message: '不存在',
    };
  }
  @Query(() => TempResults)
  async getTemps(
    @Args('page') page: PageInput,
    @CurUserId() userId: string,
    @CurOrgId() orgId: string,
    @Args('name', { nullable: true }) name?: string,
  ): Promise<TempResults> {
    const { pageNum, pageSize } = page;
    const where: FindOptionsWhere<Temp> = {
      createdBy: userId,
      org: {
        id: orgId,
      },
    };
    if (name) {
      where.name = Like(`%${name}%`);
    }
    const [results, total] = await this.tempService.findTemps({
      pageNum: (pageNum - 1) * pageSize || 0,
      pageSize,
      where,
    });
    return {
      code: SUCCESS,
      data: results,
      page: {
        pageNum,
        pageSize,
        total,
      },
      message: '获取成功',
    };
  }

  @Mutation(() => Result)
  async deleteTemp(
    @Args('id') id: string,
    @CurUserId() userId: string,
  ): Promise<Result> {
    const result = await this.tempService.findById(id);
    if (result) {
      const res = await this.tempService.deleteById(id, userId);
      if (res) {
        return {
          code: SUCCESS,
          message: '删除成功',
        };
      }
      return {
        code: null,
        message: '删除失败',
      };
    }
    return {
      code: null,
      message: '不存在',
    };
  }
}
